/**
 * 作者: wgx
 * 日期: 2024-12-20 10:07:37
 * 备注: 用户荣誉奖励的进度值和领取记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.commondata.mail.MailConstant;
import com.motu.monstercity.server.game.commondata.maintask.HonorReward;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.MailManager;
import com.motu.monstercity.server.game.logic.MainTaskManager;
import com.motu.monstercity.server.game.logic.RewardItem;
import com.motu.monstercity.server.game.logic.RewardManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;

import java.util.ArrayList;
import java.util.List;

public class UserHonor extends UserHonorModel {
    public UserHonor() {
        super();
    }

    public UserHonor(long userId, int honorType) {
        putUserId(userId);
        putHonorType(honorType);
        putResetTime(TimeUtils.getCurTime());
    }

    public void reset() {
        long curTime = TimeUtils.getCurTime();
        boolean isUpdate = false;
        if (getHonorType() == MainTaskConstant.HONOR_TYPE_101) {//  物资争夺个人荣誉 需要每周重置一次
            if (!TimeUtils.isSameWeek(curTime, getResetTime())) {
                resendSantchHonorReward(getUserId());// 通过邮件补发未领取的物资争夺荣誉奖励
                putNum(0);
                putGetReward("");
                putResetTime(curTime);
                isUpdate = true;
            }
        }
        if (isUpdate) {
            update();
        }
    }

    // 通过邮件补发未领取的物资争夺荣誉奖励
    public void resendSantchHonorReward(long userId) {
        JsonArray jsonArray = getGetRewardArray();// 领取记录
        List<RewardItem> list = new ArrayList<>();
        for (HonorReward honorReward : MainTaskManager.getHonorRewardMap().values()) {
            if (honorReward.getHonorType() == MainTaskConstant.HONOR_TYPE_101) {
                if (!Tool.isInList(jsonArray, honorReward.getId()) && getNum() >= honorReward.getNum()) {// 达标且未领取过的补发
                    List<RewardItem> tmp = CommonUtils.takeReawrdItemFromStr(honorReward.getReward());// 奖励
                    CommonUtils.mergeItemList(list, tmp);// 合并所有奖励，发送邮件
                }
            }
        }
        if (list.size() > 0) {
            MailManager.sendSystemMail(userId, MailConstant.SNATCH_HONOR_REWARD_TITLE, MailConstant.SNATCH_HONOR_REWARD_CONTENT, list, LogType.HONOR_REWARD);
        }
    }
}
